package com.whmskj.xjlhsc.controller;

import com.whmskj.xjlhsc.controller.vo.MonthlyEnergyConsumptionVO;
import com.whmskj.xjlhsc.controller.vo.ProcessYieldEnergyVO;
import com.whmskj.xjlhsc.controller.vo.Result;
import com.whmskj.xjlhsc.entity.AttendanceStat;

import com.whmskj.xjlhsc.service.AttendanceService;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

@RestController
@RequestMapping("/api/attendance")
public class AttendanceController {

    @Autowired
    private AttendanceService attendanceService;



    /**
     * 生产看板3车间的出勤情况
     * @return 出勤统计数据
     */
    @ApiOperation(value = "获取出勤统计", notes = "获取前纺、细纱、络筒三个车间的出勤情况统计")
    @GetMapping("/stats")
    public ResponseEntity<Result> getAttendanceStats() {
        try {
            List<AttendanceStat> attendanceStats = attendanceService.getAttendanceStats();
            return ResponseEntity.ok(Result.success(attendanceStats));
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error("获取出勤统计数据失败: " + e.getMessage()));
        }
    }

    /**
     * 近7日各工序产量与电耗统计
     */
    @ApiOperation(value = "近7日工序产量能耗占比", notes = "根据工序获取近7天的产量(吨)和电耗(KWh)")
    @GetMapping("/processYieldEnergy/last7Days")
    public ResponseEntity<Result> getLast7DaysYieldEnergy(@RequestParam String process) {
        try {
            if (!"前纺".equals(process) && !"细纱".equals(process) && !"络筒".equals(process)) {
                return ResponseEntity.ok(Result.error("无效的工序名称，可选值：前纺、细纱、络筒"));
            }
            List<Map<String, Object>> data = attendanceService.getLast7DaysYieldEnergy(process);
            return ResponseEntity.ok(Result.success(data));
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error("获取工序产量能耗数据失败: " + e.getMessage()));
        }
    }

//    /**
//     * 温湿度趋势：按小时返回温度/湿度
//     */
//    @ApiOperation(value = "温湿度趋势(按小时)", notes = "根据车间返回最近N小时温度/湿度趋势数据")
//    @GetMapping("/acTrend")
//    public ResponseEntity<Result> getAcTrend(@RequestParam String workshop,
//                                             @RequestParam(required = false) Integer hours) {
//        try {
//            List<Map<String, Object>> data = attendanceService.getAcTrend(workshop, hours);
//            return ResponseEntity.ok(Result.success(data));
//        } catch (Exception e) {
//            return ResponseEntity.ok(Result.error("获取温湿度趋势数据失败: " + e.getMessage()));
//        }
//    }

    @GetMapping("/acTrend")
    public ResponseEntity<Result> getAcTrend(
            @RequestParam String workshop,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate hour) { // 统一改为hour
        try {
            List<Map<String, Object>> data = attendanceService.getAcTrend(hour, workshop);
            return ResponseEntity.ok(Result.success(data));
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error("获取失败: " + e.getMessage()));
        }
    }



//    细纱看板接口 以下都是
    /**
     * 机台运行统计
     * @param workshop 车间名称 (可选)
     * @param processes
     * @return
     */
    @GetMapping("/machineStats")
    public ResponseEntity<Result> getMachineRunningStats(
            @ApiParam(value = "车间名称 (可选，与工序二选一)", example = "前纺车间")
            @RequestParam(required = false) String workshop,
            @ApiParam(value = "工序列表 (可选，与车间二选一，逗号分隔)", example = "梳棉,头并,条卷,精梳,末并,粗纱")
            @RequestParam(required = false) String processes) {
        try {
            List<String> processList = null;
            if (processes != null && !processes.isEmpty()) {
                processList = new ArrayList<>();
                String[] processArray = processes.split(",");
                for (String process : processArray) {
                    if (process != null && !process.trim().isEmpty()) {
                        processList.add(process.trim());
                    }
                }
            }

            // 如果既没有指定车间也没有指定工序，则默认查询前纺相关的工序
            if ((workshop == null || workshop.isEmpty()) && (processList == null || processList.isEmpty())) {
                processList = new ArrayList<>();
                processList.add("梳棉");
                processList.add("头并");
                processList.add("条卷");
                processList.add("精梳");
                processList.add("末并");
                processList.add("粗纱");
            }

            Map<String, Integer> data = attendanceService.getMachineRunningStats(workshop, processList);
            return ResponseEntity.ok(Result.success(data));
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error("获取机台运行统计数据失败: " + e.getMessage()));
        }
    }

    /**
     * 月能耗统计
     * @param process 工序名称 (可选，如：前纺、细纱、络筒)
     * @param date 查询日期 (可选，格式: yyyy-MM-dd，默认当前月)
     * @return 月能耗统计数据列表
     */
    @ApiOperation(value = "月能耗统计", notes = "获取指定月份各工序机台的日能耗和月总能耗")
    @GetMapping("/monthlyEnergyConsumption")
    public ResponseEntity<Result> getMonthlyEnergyConsumption(
            @ApiParam(value = "工序名称 (可选，如：前纺、细纱、络筒)", example = "细纱")
            @RequestParam(required = false) String process,
            @ApiParam(value = "查询日期 (可选，格式: yyyy-MM-dd，默认当前月)", example = "2025-08-15")
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {
        try {
            List<MonthlyEnergyConsumptionVO> data = attendanceService.getMonthlyEnergyConsumption(process, date);
            return ResponseEntity.ok(Result.success(data));
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error("获取月能耗数据失败: " + e.getMessage()));
        }
    }

}
